package com.ck.tool;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class StudentLoginFilter implements Filter {

	public void destroy() {
	}

	public void init(FilterConfig filterconfig) throws ServletException {	
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException  {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		HttpSession session = req.getSession();

		Integer Id = (Integer) session.getAttribute("studentID");
		String request_uri = req.getRequestURI().toUpperCase();// 得到用户请求的URI
		String ctxPath = req.getContextPath();// 得到web应用程序的上下文路径
		String uri = request_uri.substring(ctxPath.length()); // 得到用户请求页面
		try {
			if (request_uri.indexOf("LOGIN.HTML") == -1 && Id == null) {
				res.sendRedirect("../login.html");
				return;			
			}
			else {
				chain.doFilter(request, response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
